import Footer from "@/components/Footer.vue";
import Header from "@/components/Header.vue";
import { createRouter, createWebHashHistory } from "vue-router";
import NotFound from "@/views/NotFound/NotFound.vue";
import Top from "@/components/Top.vue";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    components: {
      default: () => import("@/views/home/home.vue"),
      footer: Footer,
    },
    meta: { title: "首页", icon: "home-o", islogin: true },
  },
  {
    path: "/category",
    components: {
      header: Header,
      default: () => import("@/views/category/category.vue"),
      footer: Footer,
    },
    meta: { title: "分类", icon: "list-switch", islogin: true },
  },
  {
    path: "/cart",
    components: {
      header: Header,
      default: () => import("@/views/cart/cart.vue"),
      footer: Footer,
    },
    meta: { title: "购物车", icon: "shopping-cart-o", islogin: true },
  },
  {
    path: "/mine",
    components: {
      header: Header,
      default: () => import("@/views/mine/mine.vue"),
      footer: Footer,
    },
    meta: { title: "我的", icon: "user-o", islogin: true },
  },
  // 登陆页
  {
    path: "/login",
    component: () => import("../views/login/login.vue"),
    meta: { title: "登录" },
  },
  // 商品详情页
  {
    path: "/details",
    components: {
      header: Top,
      default: () => import("../views/details/details.vue"),
    },
    meta: { title: "商品详情" },
  },
  // 查看所有订单页
  {
    path: "/order",
    components: {
      header: Top,
      default: () => import("../views/order/order.vue"),
    },
    meta: { title: "查看订单" },
  },
  // 提交订单页(未付款)
  {
    path: "/sborder",
    components: {
      header: Top,
      default: () => import("../views/sborder/sborder.vue"),
    },
    meta: { title: "订单详情" },
  },
  // 订单支付页面
  {
    path: "/finalorder",
    components: {
      header: Top,
      default: () => import("../views/finalorder/finalorder.vue"),
    },
    meta: { title: "订单支付" },
  },
  // 商品搜索界面
  {
    path: "/search",
    components: {
      header: Top,
      default: () => import("../views/searchshop/searchshop.vue"),
    },
    meta: { title: "搜索商品" },
  },
  // 网格商品页面
  {
    path: "/gridshop",
    components: {
      default: () => import("../views/gridshop/gridshop.vue"),
    },
    meta: {},
  },
  // 设置界面
  {
    path: "/setting",
    components: {
      header: Top,
      default: () => import("../views/settinga/settinga.vue"),
    },
    meta: { title: "设置" },
  },
  // 修改密码页面
  {
    path: "/updatepwd",
    components: {
      header: Top,
      default: () => import("../views/updatepwd/updatepwd.vue"),
    },
    meta: { title: "修改密码" },
  },
  // 地址管理
  {
    path: "/resslist",
    components: {
      header: Top,
      default: () => import("../views/resslist/resslist.vue"),
    },
    meta: { title: "地址管理" },
  },
  {
    path: "/addresslist",
    components: {
      header: Top,
      default: () => import("../views/addresslist/addresslist.vue"),
    },
    meta: { title: "新增地址" },
  },
  // 手机号管理
  {
    path: "/phone",
    components: {
      header: Top,
      default: () => import("../views/phone/phone.vue"),
    },
    meta: { title: "手机号管理" },
  },
  // 路径错误页面
  {
    path: "/:pathMatch(.*)",
    component: NotFound,
  },
  // 支付完成界面
  {
    path: "/payment",
    components: {
      header: Top,
      default: () => import("../views/payment/payment.vue"),
    },
    meta: { title: "支付界面" },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 设置路由守卫
router.beforeEach((to, from) => {
  const token = localStorage.getItem("token");
  if (!token) {
    // 没有权限的页面进行跳转到登录页
    if (to.meta.islogin) {
      return { path: "/login", query: { path: to.path } };
      // router.replace('/login')
    }
  }
});

export default router;
